package com.usian.admin.controller.v1;

import com.usian.admin.entity.AdSensitive;
import com.usian.admin.service.IAdSensitiveService;
import com.usian.common.dtos.PageResult;
import com.usian.common.dtos.ResponseResult;
import com.usian.common.enums.AppHttpCodeEnum;
import com.usian.common.exception.LeadException;
import com.usian.model.admin.dtos.SensitiveDto;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 敏感词信息表 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/api/v1/sensitive")
@Api(tags = "敏感词信息表接口")
@CrossOrigin
public class AdSensitiveController{

    @Autowired
    private IAdSensitiveService adSensitiveService;

    /**
     * 分页条件查询敏感词列表
     */
    @PostMapping("/list")
    public PageResult<AdSensitive> findByPage(@RequestBody SensitiveDto dto){
        return adSensitiveService.findByPage(dto);
    }

    /**
     * 新增敏感词
     */
    @PostMapping("/save")
    public ResponseResult save(@RequestBody AdSensitive adSensitive){
        if(StringUtils.isBlank(adSensitive.getSensitives())){
            throw new LeadException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //设置创建时间
        adSensitive.setCreatedTime(new Date());
        boolean b = adSensitiveService.save(adSensitive);
        if(!b){
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.ok();
    }

    /**
     * 修改敏感词
     */
    @PutMapping("/update")
    public ResponseResult update(@RequestBody AdSensitive adSensitive){
        boolean b = adSensitiveService.updateById(adSensitive);
        if(!b){
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.ok();
    }
    /**
     * 删除敏感词
     */
    @DeleteMapping("/del/{id}")
    public ResponseResult delete(@PathVariable("id") Integer id){
        if(id==null){
            throw new LeadException(AppHttpCodeEnum.PARAM_INVALID);
        }
        boolean b = adSensitiveService.removeById(id);
        if(!b){
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.ok();
    }

}
